﻿using System;
using System.Collections.Generic;
using System.Text;
using RockCompany.Common.Lib;
using RockCompany.Master.DTO;
using RockCompany.Common.Constant;
using System.Windows.Forms;
using System.Data;

namespace RockCompany.Master.Model
{
    public class SI03AFormModel : CommonModel
    {
        #region Properties
        private DataTable newMatCheckDetails;
        public DataTable NewMatCheckDetails
        {
            get { return newMatCheckDetails; }
            set { newMatCheckDetails = value; }
        }
        private int nNewRow = 0;
        #endregion

        #region Constructor
        public SI03AFormModel()
        {
            dbConnector = new DBConnector();
            DataList = new List<object>();
            NewMatCheckDetails = new DataTable();
        }
        #endregion

        #region Utility Methods
        public DataTable supplierLOV()
        {
            string sClause = "SELECT " + PHIEU_KN_HN_DTO.N_NHA_CUNG_UNG +
                             " FROM " + PHIEU_KN_HN_DTO.N_TABLE +
                             " GROUP BY " + PHIEU_KN_HN_DTO.N_NHA_CUNG_UNG;
            return selectData(sClause);
        }

        public DataTable newOrderLOV()
        {
            string sClause = "SELECT " + PHIEU_KN_HN_DTO.N_MS_DON_HANG +
                             " FROM " + PHIEU_KN_HN_DTO.N_TABLE +
                             " GROUP BY " + PHIEU_KN_HN_DTO.N_MS_DON_HANG;
            return selectData(sClause);
        }

        public DataRow getCheckedRow(int index)
        {
            DataRow curRow = newMatCheckDetails.Rows[index];
            curRow.SetField<bool>(PHIEU_KN_HN_CT_DTO.N_CHON, !curRow.Field<bool>(PHIEU_KN_HN_CT_DTO.N_CHON));
            return curRow;
        }

        private DataTable getMatsInfo(DataTable storedMats)
        {
            string sMatsList = "";
            foreach (DataRow row in storedMats.Rows)
            {
                sMatsList += "'" + Util.parseString(row[NGUYEN_VLIEU_VTU_DTO.N_MA_NVL_VTU]) + "',";
            }
            sMatsList = sMatsList.Substring(0, sMatsList.Length - 1);

            string sSql = "SELECT A1." + TON_KHO_DTO.N_MA_HANG + " AS '" + NGUYEN_VLIEU_VTU_DTO.N_MA_NVL_VTU + "',";
            sSql += "ISNULL(B1." + NGUYEN_VLIEU_VTU_DTO.N_TEN_NVL_VTU + ",'') AS '" + NGUYEN_VLIEU_VTU_DTO.N_TEN_NVL_VTU + "',";
            sSql += "ISNULL(B1." + NGUYEN_VLIEU_VTU_DTO.N_DON_VI + ",'') AS '" + NGUYEN_VLIEU_VTU_DTO.N_DON_VI + "'";
            sSql += " FROM((SELECT *";
            sSql += " FROM " + TON_KHO_DTO.N_TABLE;
            sSql += " WHERE " + TON_KHO_DTO.N_MA_HANG + " IN(" + sMatsList + ")) A1";
            sSql += " LEFT JOIN(SELECT *";
            sSql += " FROM " + NGUYEN_VLIEU_VTU_DTO.N_TABLE + ") B1";
            sSql += " ON A1." + TON_KHO_DTO.N_MA_HANG + "= B1." + NGUYEN_VLIEU_VTU_DTO.N_MA_NVL_VTU + ") ";
            return selectData(sSql);
        }
        #endregion

        #region Main Methods
        public bool addNewMatCheckDetail(string matCheckNo, DataTable sMatCheckDetails)
        {
            int nRow = 0;
            DataTable matsInfo = getMatsInfo(sMatCheckDetails);
            foreach (DataRow curRow in matsInfo.Rows)
            {
                DataRow newRow = NewMatCheckDetails.NewRow();
                newRow.SetField<bool>(PHIEU_KN_HN_CT_DTO.N_CHON, false);
                newRow.SetField<string>(NGUYEN_VLIEU_VTU_DTO.N_MA_NVL_VTU, Util.parseString(curRow[NGUYEN_VLIEU_VTU_DTO.N_MA_NVL_VTU]));
                newRow.SetField<string>(NGUYEN_VLIEU_VTU_DTO.N_TEN_NVL_VTU, Util.parseString(curRow[NGUYEN_VLIEU_VTU_DTO.N_TEN_NVL_VTU]));
                newRow.SetField<string>(NGUYEN_VLIEU_VTU_DTO.N_DON_VI, Util.parseString(curRow[NGUYEN_VLIEU_VTU_DTO.N_DON_VI]));
                newRow.SetField<double>(PHIEU_KN_HN_CT_DTO.N_SL_DAT_MUA, 0);
                newRow.SetField<double>(PHIEU_KN_HN_CT_DTO.N_SL_THUC_NHAN, 0);
                newRow.SetField<double>(PHIEU_KN_HN_CT_DTO.N_SL_GIAO_HANG, 0);
                newRow.SetField<string>(PHIEU_KN_HN_CT_DTO.N_GHI_CHU, "");
                newMatCheckDetails.Rows.Add(newRow);
                nRow++;
            }
            nNewRow = nRow;
            return nRow == sMatCheckDetails.Rows.Count;
        }

        public bool removeMatCheckDetail(string sMatCheckNo)
        {
            string sWhereClause = " WHERE ";
            sWhereClause += PHIEU_KN_HN_DTO.N_MS_PHIEU_KN + " ='" + sMatCheckNo + "' AND ";
            sWhereClause += PHIEU_KN_HN_CT_DTO.N_MA_NVL_VTU + " IN(";
            Dictionary<string, object> pv = new Dictionary<string, object>();
            foreach (DataRow curRow in DataList)
            {
                if (curRow.RowState != DataRowState.Added)
                {
                    string sP = ParaConst.P_MA_NVL_VTU + DataList.IndexOf(curRow).ToString();
                    sWhereClause += sP + ",";
                    pv.Add(sP, curRow.Field<string>(PHIEU_KN_HN_CT_DTO.N_MA_NVL_VTU));
                }
                newMatCheckDetails.Rows.Remove(curRow);
            }
            DataList.Clear();
            if (pv.Count > 0)
            {
                sWhereClause = sWhereClause.Substring(0, sWhereClause.Length - 1) + ")";
                if (removeRow(PHIEU_KN_HN_CT_DTO.N_TABLE, sWhereClause, pv))
                    pv.Clear();
            }
            return pv.Count == 0;
        }

        public bool insertNewMatCheckDetail(string sMatCheckNo,string suserrName, DataRow newRow)
        {
            Dictionary<string, object> pv = new Dictionary<string, object>();
            pv.Add(PHIEU_KN_HN_DTO.N_MS_PHIEU_KN, sMatCheckNo);
            pv.Add(PHIEU_KN_HN_CT_DTO.N_SL_DAT_MUA, newRow[PHIEU_KN_HN_CT_DTO.N_SL_DAT_MUA]);
            pv.Add(PHIEU_KN_HN_CT_DTO.N_SL_GIAO_HANG, newRow[PHIEU_KN_HN_CT_DTO.N_SL_GIAO_HANG]);
            pv.Add(PHIEU_KN_HN_CT_DTO.N_SL_THUC_NHAN, newRow[PHIEU_KN_HN_CT_DTO.N_SL_THUC_NHAN]);
            pv.Add(PHIEU_KN_HN_CT_DTO.N_MA_NVL_VTU, newRow[PHIEU_KN_HN_CT_DTO.N_MA_NVL_VTU]);
            pv.Add(PHIEU_KN_HN_CT_DTO.N_GHI_CHU, newRow[PHIEU_KN_HN_CT_DTO.N_GHI_CHU]);

            pv.Add(PHIEU_KN_HN_CT_DTO.N_CREATION_PROGRAM_ID, "SI03A");
            pv.Add(PHIEU_KN_HN_CT_DTO.N_CREATION_TIME, DateTime.Now);
            pv.Add(PHIEU_KN_HN_CT_DTO.N_CREATION_USER, suserrName);
            return insertData(PHIEU_KN_HN_CT_DTO.N_TABLE, pv);
        }

        public bool insertNewMatCheck(PHIEU_KN_HN_DTO curMatCheck)
        {
            Dictionary<string, object> pv = new Dictionary<string, object>();
            pv.Add(PHIEU_KN_HN_DTO.N_MS_PHIEU_KN, curMatCheck.MS_PHIEU_KN);
            pv.Add(PHIEU_KN_HN_DTO.N_PHE_DUYET, curMatCheck.PHE_DUYET);
            pv.Add(PHIEU_KN_HN_DTO.N_MS_HOA_DON, curMatCheck.MS_HOA_DON);
            pv.Add(PHIEU_KN_HN_DTO.N_MS_DON_HANG, curMatCheck.MS_DON_HANG);
            pv.Add(PHIEU_KN_HN_DTO.N_NHA_CUNG_UNG, curMatCheck.NHA_CUNG_UNG);
            pv.Add(PHIEU_KN_HN_DTO.N_NGAY_GIAO_HANG, curMatCheck.NGAY_GIAO_HANG);

            pv.Add(PHIEU_KN_HN_DTO.N_CREATION_TIME, DateTime.Now);
            pv.Add(PHIEU_KN_HN_DTO.N_CREATION_PROGRAM_ID, "SI03A");
            pv.Add(PHIEU_KN_HN_DTO.N_CREATION_USER, curMatCheck.CREATION_USER);
            return insertData(PHIEU_KN_HN_DTO.N_TABLE, pv);
        }

        public bool updateMatCheck(PHIEU_KN_HN_DTO curMatCheck)
        {
            string sSetClause = "\nSET\n";
            sSetClause += PHIEU_KN_HN_DTO.N_PHE_DUYET + " = " + ParaConst.P_PHE_DUYET + ",\n";
            sSetClause += PHIEU_KN_HN_DTO.N_MS_HOA_DON + " = " + ParaConst.P_MS_HOA_DON + ",\n";
            sSetClause += PHIEU_KN_HN_DTO.N_MS_DON_HANG + " = " + ParaConst.P_MS_DON_HANG + ",\n";
            sSetClause += PHIEU_KN_HN_DTO.N_NHA_CUNG_UNG + " = " + ParaConst.P_NHA_CUNG_UNG + ",\n";
            sSetClause += PHIEU_KN_HN_DTO.N_LAST_UPDATE_TIME + " = " + ParaConst.P_LAST_UPDATE_TIME + ",\n";
            sSetClause += PHIEU_KN_HN_DTO.N_LAST_UPDATE_PROGRAM_ID + " = " + ParaConst.P_LAST_UPDATE_PROGRAM_ID + ",\n";
            sSetClause += PHIEU_KN_HN_DTO.N_LAST_UPDATE_USER + " = " + ParaConst.P_LAST_UPDATE_USER + "\n";

            string sClause = "WHERE ";
            sClause += PHIEU_KN_HN_DTO.N_MS_PHIEU_KN + " = " + ParaConst.P_MS_PHIEU_KN;
            Dictionary<string, object> updateValues = new Dictionary<string, object>();
            updateValues.Add(ParaConst.P_MS_PHIEU_KN, curMatCheck.MS_PHIEU_KN);
            updateValues.Add(ParaConst.P_PHE_DUYET, curMatCheck.PHE_DUYET);
            updateValues.Add(ParaConst.P_MS_HOA_DON, curMatCheck.MS_HOA_DON);
            updateValues.Add(ParaConst.P_MS_DON_HANG, curMatCheck.MS_DON_HANG);
            updateValues.Add(ParaConst.P_NHA_CUNG_UNG, curMatCheck.NHA_CUNG_UNG);
            updateValues.Add(ParaConst.P_LAST_UPDATE_TIME, DateTime.Now);
            updateValues.Add(ParaConst.P_LAST_UPDATE_PROGRAM_ID, "SI03A");
            updateValues.Add(ParaConst.P_LAST_UPDATE_USER, curMatCheck.CREATION_USER);
            return updateData(PHIEU_KN_HN_DTO.N_TABLE, sSetClause, updateValues, sClause);
        }

        public bool updateMatCheckDetail(string sMatCheck,DataRow modRow)
        {
            string sSetClause = " SET ";
            sSetClause += PHIEU_KN_HN_CT_DTO.N_MA_NVL_VTU + " = " + ParaConst.P_MA_NVL_VTU + ",\n";
            sSetClause += PHIEU_KN_HN_CT_DTO.N_SL_DAT_MUA + " = " + ParaConst.P_SL_DAT_MUA + ",\n";
            sSetClause += PHIEU_KN_HN_CT_DTO.N_SL_THUC_NHAN + " = " + ParaConst.P_SL_THUC_NHAN + ",\n";
            sSetClause += PHIEU_KN_HN_CT_DTO.N_SL_GIAO_HANG + " = " + ParaConst.P_SL_GIAO_HANG + ",\n";
            sSetClause += PHIEU_KN_HN_CT_DTO.N_GHI_CHU + " = @" + PHIEU_KN_HN_CT_DTO.N_GHI_CHU + ",\n";

            sSetClause += PHIEU_KN_HN_DTO.N_LAST_UPDATE_TIME + " = " + ParaConst.P_LAST_UPDATE_TIME + ",\n";
            sSetClause += PHIEU_KN_HN_DTO.N_LAST_UPDATE_PROGRAM_ID + " = " + ParaConst.P_LAST_UPDATE_PROGRAM_ID + ",\n";
            sSetClause += PHIEU_KN_HN_DTO.N_LAST_UPDATE_USER + " = " + ParaConst.P_LAST_UPDATE_USER + "\n";

            string sClause = "WHERE ";
            sClause += PHIEU_KN_HN_DTO.N_MS_PHIEU_KN + " = " + ParaConst.P_MS_PHIEU_KN;
            sClause += " AND " + PHIEU_KN_HN_CT_DTO.N_MA_NVL_VTU + " = " + ParaConst.P_MA_NVL_VTU;

            Dictionary<string, object> updateValues = new Dictionary<string, object>();
            updateValues.Add(ParaConst.P_MS_PHIEU_KN, sMatCheck);
            updateValues.Add(ParaConst.P_MA_NVL_VTU, modRow.Field<string>(PHIEU_KN_HN_CT_DTO.N_MA_NVL_VTU));
            updateValues.Add(ParaConst.P_SL_DAT_MUA, Util.parseDecimal(modRow[PHIEU_KN_HN_CT_DTO.N_SL_DAT_MUA]));
            updateValues.Add(ParaConst.P_SL_THUC_NHAN, Util.parseDecimal(modRow[PHIEU_KN_HN_CT_DTO.N_SL_THUC_NHAN]));
            updateValues.Add(ParaConst.P_SL_GIAO_HANG, Util.parseDecimal(modRow[PHIEU_KN_HN_CT_DTO.N_SL_GIAO_HANG]));
            updateValues.Add("@" + PHIEU_KN_HN_CT_DTO.N_GHI_CHU, modRow.Field<string>(PHIEU_KN_HN_CT_DTO.N_GHI_CHU));
            updateValues.Add(ParaConst.P_LAST_UPDATE_TIME, DateTime.Now);
            updateValues.Add(ParaConst.P_LAST_UPDATE_PROGRAM_ID, "SI03A");
            updateValues.Add(ParaConst.P_LAST_UPDATE_USER, "");
            return updateData(PHIEU_KN_HN_CT_DTO.N_TABLE, sSetClause, updateValues, sClause); 
        }

        public bool saveData(string sMatCheckNo, string sUserName)
        {
            DataTable tempDb = null;
            int insertRes = 0, updateRes = 0;

            tempDb = newMatCheckDetails.GetChanges(DataRowState.Modified);
            int updatedRowCount = (tempDb != null ? tempDb.Rows.Count : 0);

            tempDb = newMatCheckDetails.GetChanges(DataRowState.Added);
            int addedRowCount = (tempDb != null ? tempDb.Rows.Count : 0);

            tempDb = newMatCheckDetails.GetChanges();
            if (tempDb != null && tempDb.Rows.Count > 0)
            {
                foreach (DataRow curRow in tempDb.Rows)
                {
                    switch (curRow.RowState)
                    {
                        case DataRowState.Added:
                            if (insertNewMatCheckDetail(sMatCheckNo, sUserName, curRow))
                                insertRes++;
                            break;
                        case DataRowState.Modified:
                            if (updateMatCheckDetail(sMatCheckNo, curRow))
                                updateRes++;
                            break;
                    }
                }
                newMatCheckDetails.AcceptChanges();
            }
            return insertRes == addedRowCount && updateRes == updatedRowCount;
        }

        #endregion
    }
}
